package com.baturu.vin.hyundai.dto;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 */
@FieldDefaults(level = AccessLevel.PRIVATE)
public class HyundaiKiaAttributeTransformDTO implements Function<String, Map<String, Object>> {

    Pattern pattern;

    Function<Matcher, Map<String, Object>> transformFunction;

    public HyundaiKiaAttributeTransformDTO(String regex, Function<Matcher, Map<String, Object>> transformFunction) {
        this.pattern = Pattern.compile(regex);
        this.transformFunction = transformFunction;
    }

    @Override
    public Map<String, Object> apply(String describe) {
        Matcher matcher = pattern.matcher(describe);
        if (matcher.find()) {
            return Optional.of(matcher)
                    .map(transformFunction)
                    .orElse(null);
        } else {
            return null;
        }
    }
}